<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      title="分享商品二维码"
      width="700px"
      @close="close"
    >
      <div v-loading="loading">
        <div class="filter-container">
          <el-form ref="form" :model="form" size="small" label-width="90px">
            <el-form-item v-if="row" label="分享商品：">{{
              row.goods.title
            }}</el-form-item>
            <el-form-item label="选择门店：">
              <el-select
                v-model="form.storeId"
                filterable
                placeholder="可通过名称搜索"
                @change="storeIdChange"
              >
                <el-option
                  v-for="(item, index) in shopOptions"
                  :key="index"
                  :label="item.storeName"
                  :value="item.storeId"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <div class="content-container">
          <div v-if="qrCodeUrl" class="qr-code-box">
            <el-image :src="qrCodeUrl" />
          </div>
          <div v-if="maInfo.DEFAULT && row" class="wx-ma-box">
            <div>
              <div class="cell-item">
                <span>小程序原始ID：</span>
                <span>{{ maInfo.DEFAULT.auth.base.userName }}</span>
              </div>
              <div class="cell-item">
                <span>小程序APPID：</span>
                <span>{{ maInfo.DEFAULT.auth.base.appId }}</span>
              </div>
              <div class="cell-item">
                <span>小程序路径：</span>
                <span>sp-mall/pages/goods-detail/index?storeId={{
                  this.form.storeId
                }}&goodsSerial={{ row.goods.goodsSerial }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  productSkuStoreList,
  shareProductSkuQrCode
} from '@/views/wxaMallModule/api/sku'
export default {
  data() {
    return {
      // 配置
      resolve: null,
      reject: null,
      shopOptions: [],
      //  状态
      dialogVisible: null,
      loading: false,
      // 数据
      row: '',
      qrCodeUrl: '',
      identityCode: '',
      form: {
        storeId: ''
      }
    }
  },
  computed: {
    ...mapGetters(['maInfo'])
  },
  methods: {
    open({ row, shopOptions }) {
      this.row = row
      this.getSkuInfo()
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 获取数据
       */
    // 获取sku列表
    getSkuInfo() {
      this.loading = true
      productSkuStoreList({ skuId: this.row.skuInfo.skuId })
        .then((res) => {
          const list = res.data || []
          for (let i = 0; i < list.length; i++) {
            if (!list[i].status) {
              list.splice(i, 1)
              i--
            }
          }
          this.shopOptions = list
          if (this.shopOptions.length) {
            this.form.storeId = list[0].storeId
            this.fetchData()
          }
        })
        .catch((err) => {
          this.loading = false
        })
        .finally(() => {})
    },

    // 获取二维码
    fetchData() {
      this.loading = true
      const params = {
        appId: this.$store.state.wechat.maInfo.DEFAULT.appId,
        goodsId: this.row.goods.goodsId,
        storeId: this.form.storeId,
        skuId: this.row.skuInfo.skuId
      }
      shareProductSkuQrCode(params)
        .then((res) => {
          this.qrCodeUrl = res.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    /**
       * 状态
       */
    // 门店id改变
    storeIdChange(val) {
      this.fetchData()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .filter-container {
    margin-bottom: 10px;
    ::v-deep .el-select {
      width: 100%;
    }
  }
  .content-container {
    display: flex;
    .qr-code-box {
      width: 220px;
      height: 220px;
      margin: 0 20px 20px 0;
    }
    .wx-ma-box {
      display: flex;
      flex: 1;
      align-items: center;
    }
    .cell-item {
      display: flex;
      flex: 1;
      margin-bottom: 10px;
      span:first-child {
        flex: 0 0 120px;
        text-align: right;
      }
      span:last-child {
        // word-break: break-all;
      }
    }
  }
</style>
